package name.matthewgreet.example2.util; import java.math.BigDecimal; import java.math.RoundingMode; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; import com.opensymphony.xwork2.interceptor.ValidationAware; import name.matthewgreet.example2.action.ViewGameListAction.GameCategory; import name.matthewgreet.example2.dto.DesignerDTO; public class FormatLibrary { private static final String NUMBER_FORMAT = "##,###,###"; public static String formatCategoriesText(boolean hasAbstract, boolean hasChildren, boolean hasCustomizable, boolean hasFamily, boolean hasParty, boolean hasStrategy, boolean hasThematic, boolean hasWar) { String result; result = ""; if (hasAbstract) { if (result.length() > 0) result += ", "; result += formatGameCategory(GameCategory.ABSTRACT); } if (hasChildren) { if (result.length() > 0) result += ", "; result += formatGameCategory(GameCategory.CHILDREN); } if (hasCustomizable) { if (result.length() > 0) result += ", "; result += formatGameCategory(GameCategory.CUSTOMIZABLE); } if (hasFamily) { if (result.length() > 0) result += ", "; result += formatGameCategory(GameCategory.FAMILY); } if (hasParty) { if (result.length() > 0) result += ", "; result += formatGameCategory(GameCategory.PARTY); } if (hasStrategy) { if (result.length() > 0) result += ", "; result += formatGameCategory(GameCategory.STRATEGY); } if (hasThematic) { if (result.length() > 0) result += ", "; result += formatGameCategory(GameCategory.THEMATIC); } if (hasWar) { if (result.length() > 0) result += ", "; result += formatGameCategory(GameCategory.WAR); } return result; } public static String formatDesignersText(Collection<DesignerDTO> designers, boolean hasOthers, boolean useHTMLNonBreakingSpace) { List<DesignerDTO> sortedDesigners; String result; sortedDesigners = new ArrayList<>(designers); Collections.sort(sortedDesigners, (d1,d2) -> d1.getName().compareToIgnoreCase(d2.getName())); result = ""; if (designers != null) { for (DesignerDTO designer: sortedDesigners) { if (result.length() > 0) result += ", "; if (useHTMLNonBreakingSpace) { result += designer.getName().replace(" ", " "); } else { result += designer.getName(); } } if (hasOthers) { if (result.length() > 0) result += ", "; result += "Others"; } } return result; } public static String formatGameCategory(GameCategory preferredCategory) { switch (preferredCategory) { case ABSTRACT: return "Abstract"; case ALL: return "Overall"; case CHILDREN: return "Children's"; case CUSTOMIZABLE: return "Customizable"; case FAMILY: return "Family"; case PARTY: return "Party"; case STRATEGY: return "Strategy"; case THEMATIC: return "Thematic"; case WAR: return "War"; } return ""; } public static String formatGameWeight(BigDecimal gameWeight) { return gameWeight.setScale(1, RoundingMode.HALF_UP).toString(); } public static String formatGameWeightDesc(BigDecimal gameWeight) { String result; if (gameWeight.floatValue() >= 1.0 && gameWeight.floatValue() < 2.0) { result = "Light"; } else if (gameWeight.floatValue() >= 2.0 && gameWeight.floatValue() < 3.0) { result = "Light-Medium"; } else if (gameWeight.floatValue() >= 3.0 && gameWeight.floatValue() < 4.0) { result = "Medium"; } else if (gameWeight.floatValue() >= 4.0 && gameWeight.floatValue() < 4.5) { result = "Medium-Heavy"; } else if (gameWeight.floatValue() >= 4.5 && gameWeight.floatValue() < 5) { result = "Heavy"; } else { result = ""; } return result; } public static String formatNumber(Integer value) { if (value != null) { return (new DecimalFormat(NUMBER_FORMAT)).format(value); } else { return ""; } } public static String formatUserRating(BigDecimal userRating) { return userRating.setScale(1, RoundingMode.HALF_UP).toString(); } public static String formatUserRatingDesc(BigDecimal userRating) { String result; result = ""; switch (userRating.intValue()) { case 0: result = "Unrated"; break; case 1: result = "Awful - defies game description"; break; case 2: result = "Very Bad - won't play ever again"; break; case 3: result = "Bad - likely won't play this again"; break; case 4: result = "Not so good - but could play again"; break; case 5: result = "Mediocre - take it or leave it"; break; case 6: result = "Ok - will play if in the mood"; break; case 7: result = "Good - usually willing to play"; break; case 8: result = "Very good - enjoy playing and would suggest it"; break; case 9: result = "Excellent - very much enjoy playing"; break; case 10: result = "Outstanding - will always enjoy playing"; break; } return result; } public static<T extends Enum<T>> T parseRequiredEnum(String field, Class<T> enumType, ValidationAware validationAware, String requiredMessage, String notEnumMessage) { T result = null; if (field == null || field.length() == 0) { validationAware.addActionError(requiredMessage); } else { try { result = Enum.valueOf(enumType, field); } catch (IllegalArgumentException e) { validationAware.addActionError(notEnumMessage); } } return result; } public static Integer parseInteger(String value, ValidationAware validationAware, String notNumberMessage) { if (value != null && value.trim().length() > 0) { try { return Integer.parseInt(value); } catch (NumberFormatException e) { validationAware.addActionError(notNumberMessage); } } return null; } }